package com.java.mogodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import java.util.Scanner;


public class MongoDb {
    public static void main(String[] args) throws Exception {
        System.out.println("1. Insert\n2. delete\n3. update\n4. find\n5. Display All");

        MongoDb.dataConnecting();
    }
    public static void dataConnecting(){
        // Connect to MongoDB
        try{

            MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
            MongoDatabase database = mongoClient.getDatabase("admin"); // Just the database name, not the connection string
            Scanner sc = new Scanner(System.in);
            String Connect= "sampleCollection";

            int choice = Integer.parseInt(sc.next());
            switch (choice){
                case 1->{
                    //Insert a document
                    System.out.println("Enter the your name");
                    String name = sc.next();
                    System.out.println("Enter your city");
                    String city = sc.next();
                    System.out.println("Enter your email");
                    String email = sc.next();
                    database.getCollection(Connect)
                            .insertOne(new Document("Name",name).append("City ",city).append("Email",email));
                    database.getCollection(Connect).find().forEach(doc-> System.out.println(doc.toJson()));
                }
                case 2-> {
                    //deleting a document
                    System.out.println("Enter the your name");
                    String name= sc.next();
                    Document deleteDoc= database.getCollection(Connect)
                            .findOneAndDelete(Filters.eq("name",name));

                              if(deleteDoc!=null){
                                System.out.println("deleted count: "+deleteDoc.toJson());
                              }else {
                                System.out.println("please enter correct name to delete");
                              }

                }
                case 3->{
                    //updating a document
                    System.out.println("Enter the current name ");
                    String curName = sc.next();
                    System.out.println("Enter the new name:");
                    String newName = sc.next().trim();
                    Document  updatedOne= database.getCollection(Connect)
                            .findOneAndUpdate(Filters.eq("Name",curName), Updates.set("Name",newName));

                   if(updatedOne!=null){

                       System.out.println("Document modified :"+updatedOne);
                   }else {
                       System.out.println("please enter correct name to update");

                   }
                }case 4->{
                    // find a document
                    System.out.println("Enter name to find details");
                    String name = sc.next().trim();
                    if (name!=null){
                        System.out.println("please Enter Correct Name:");
                    }else {
                        database.getCollection(Connect).find(Filters.eq("Name",name)).forEach(doc-> System.out.println(doc.toJson()));
                    }
                }
                case 5 -> {
                    //displaying all documents from Mongodb
                    if (database!=null){
                        System.out.println("This Collection has no data");
                    }else {
                        database.getCollection(Connect).find().forEach(doc-> System.out.println(doc.toJson()));
                    }
                }
                default -> System.out.println("Invalid option");
            }

        }catch (Exception e ){
            System.out.println("Error :"+e.getMessage());
        }

    }
}
